﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Praxis.Entities;
using Praxis.BusinessLogic;

namespace Praxis.UI
{
    public partial class FrmMttoObra : Form
    {

        [Dependency]
        public IObraBL obraBL { get; set; }

        bool bandera;

        public FrmMttoObra()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public OBRA CurrentObra { get; set; }

        public void Nuevo()
        {
            bandera = true;
            oBRABindingSource.AddNew();
        }

        public void Editar()
        {
            bandera = false;
            oBRABindingSource.DataSource = CurrentObra;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OBRA currentObra = oBRABindingSource.Current as OBRA;
            var msg = "ERROR";

            if (currentObra != null)
            {
                if (bandera == true)
                {
                    try
                    {
                        obraBL.AddObra(currentObra);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
                }
                if (bandera == false)
                {
                    try
                    {
                        obraBL.UpdateObra(currentObra);
                        MessageBox.Show("Modificacion Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
                }
            }
            
        }

        private void FrmMttoObra_Load(object sender, EventArgs e)
        {

        }
    }
}
